package database;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

import util.DateTimeHelper;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import entity.FTodo;

public class FTodoEntities {
	private static FTodoEntities instance = null;
	private DatabaseHelper databaseHelper;

	public static FTodoEntities getInstance(Context context) {
		if(instance == null) {
			instance = new FTodoEntities(context);
		}
		
		return instance;
	}
	
	private FTodoEntities(Context context) {
		databaseHelper = DatabaseHelper.getInstance(context);
	}

	public ArrayList<Integer> getListDate(int dateFrom, int dateTo) {
		ArrayList<Integer> dates = new ArrayList<Integer>();
		try {
			dates = new ArrayList<Integer>();
			Cursor c = databaseHelper.getListDate(dateFrom, dateTo);
			if (c.moveToFirst()) {
				do {
					int date = c.getInt(c.getColumnIndex(Config.COL_DATE));
					dates.add(date);
				} while (c.moveToNext());
			}
			c.close();
		} catch (SQLException ex) {
			dates.clear();
		} 
		Collections.sort(dates);
		
		return dates;
	}

	public ArrayList<FTodo> getListFTodoByDate(int date) {
		ArrayList<FTodo> fTodos = new ArrayList<FTodo>();
		try {
			Cursor c = databaseHelper.getListFTodoByDate(date);
			if (c.moveToFirst()) {
				do {
					FTodo ftodo = new FTodo();
					ftodo.id = c.getInt(c.getColumnIndex(Config.COL_ID));
					ftodo.taskname = c.getString(c
							.getColumnIndex(Config.COL_TASKNAME));
					ftodo.priority = c.getInt(c
							.getColumnIndex(Config.COL_PRIORITY));
					ftodo.isFinish = c.getInt(c
							.getColumnIndex(Config.COL_ISFINISH));
					ftodo.dateCreate = c.getInt(c
							.getColumnIndex(Config.COL_DATE));
					ftodo.timeCreate = c.getInt(c
							.getColumnIndex(Config.COL_TIME));
					ftodo.reminder = c.getString(c
							.getColumnIndex(Config.COL_REMINDER));
					ftodo.place = c.getString(c
							.getColumnIndex(Config.COL_PLACE));
					ftodo.people = c.getString(c
							.getColumnIndex(Config.COL_PEOPLE));
					fTodos.add(ftodo);
				} while (c.moveToNext());
			}
			c.close();
			Collections.sort(fTodos, FTodo.getComparator(
					FTodo.SortParameter.FINISH_ASCENDING,
					FTodo.SortParameter.PRIORITY_DESCENDING,
					FTodo.SortParameter.TASKNAME_ASCENDING));
		} catch (Exception ex) {
			fTodos.clear();
		}
		
		return fTodos;
	}
	
	public ArrayList<ArrayList<FTodo>> getListFTodoDate(
			ArrayList<Integer> dates) {
		ArrayList<ArrayList<FTodo>> lstFTodos = new ArrayList<ArrayList<FTodo>>();
		try {
			for (int i : dates) {
				ArrayList<FTodo> arr = getListFTodoByDate(i);
				lstFTodos.add(arr);
			}
		} catch (SQLException ex) {
			lstFTodos.clear();
		} 
		
		return lstFTodos;
	}

	public boolean insertFTodo(FTodo ftodo) {
		boolean result = false;
		try {
			result = databaseHelper.insertFTodo(ftodo);
		} catch (SQLException ex) {
			result = false;
		}
		
		return result;
	}

	public boolean deleteFTodo(int id) {
		boolean result = false;
		try {
			result = databaseHelper.deleteFtodo(id);
		} catch (SQLException ex) {
			result = false;
		}
		
		return result;
	}

	public boolean updateFTodo(FTodo ftodo) {
		boolean result = false;
		try {
			result = databaseHelper.updateFTodo(ftodo);
		} catch (SQLException ex) {
			result = false;
		} 
		
		return result;
	}

	public ArrayList<FTodo> getListFTodoByTaskName(String taskName) {
		ArrayList<FTodo> lst = new ArrayList<FTodo>();
		try {
			int date = DateTimeHelper.formatDateAsInt(Calendar.getInstance());
			Cursor c = databaseHelper.getListByTaskName(taskName, date);
			if (c.moveToFirst()) {
				do {
					FTodo ftodo = new FTodo();
					ftodo.id = c.getInt(c.getColumnIndex(Config.COL_ID));
					ftodo.taskname = c.getString(c
							.getColumnIndex(Config.COL_TASKNAME));
					ftodo.priority = c.getInt(c
							.getColumnIndexOrThrow(Config.COL_PRIORITY));
					ftodo.isFinish = c.getInt(c
							.getColumnIndex(Config.COL_ISFINISH));
					ftodo.dateCreate = c.getInt(c
							.getColumnIndex(Config.COL_DATE));
					ftodo.timeCreate = c.getInt(c
							.getColumnIndex(Config.COL_TIME));
					ftodo.reminder = c.getString(c
							.getColumnIndex(Config.COL_REMINDER));
					ftodo.place = c.getString(c
							.getColumnIndex(Config.COL_PLACE));
					ftodo.people = c.getString(c
							.getColumnIndex(Config.COL_PEOPLE));
					lst.add(ftodo);
				} while (c.moveToNext());
			}
			c.close();
			Collections.sort(lst, FTodo.getComparator(
					FTodo.SortParameter.FINISH_ASCENDING,
					FTodo.SortParameter.PRIORITY_DESCENDING,
					FTodo.SortParameter.TASKNAME_ASCENDING));
		} catch (SQLException ex) {
			lst.clear();
		} 
		
		return lst;
	}

	public ArrayList<FTodo> getAllFTodo() {
		ArrayList<FTodo> lst = new ArrayList<FTodo>();
		try {
			Cursor c = databaseHelper.getAllFTodo();
			if (c.moveToFirst()) {
				do {
					FTodo ftodo = new FTodo();
					ftodo.id = c.getInt(c.getColumnIndex(Config.COL_ID));
					ftodo.taskname = c.getString(c
							.getColumnIndex(Config.COL_TASKNAME));
					ftodo.priority = c.getInt(c
							.getColumnIndex(Config.COL_PRIORITY));
					ftodo.isFinish = c.getInt(c
							.getColumnIndex(Config.COL_ISFINISH));
					ftodo.dateCreate = c.getInt(c
							.getColumnIndex(Config.COL_DATE));
					ftodo.timeCreate = c.getInt(c
							.getColumnIndex(Config.COL_TIME));
					ftodo.reminder = c.getString(c
							.getColumnIndex(Config.COL_REMINDER));
					ftodo.place = c.getString(c
							.getColumnIndex(Config.COL_PLACE));
					ftodo.people = c.getString(c
							.getColumnIndex(Config.COL_PEOPLE));
					lst.add(ftodo);
				} while (c.moveToNext());
			}
		} catch (SQLException ex) {
			lst.clear();
		} 
		
		return lst;
	}
}
